var _ajax = require('../../utils/ajax');
var _Server = require('../../utils/server');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        upImgBool: true,
        imgArray: [],
        imgUpArray: [], // 服务器返回图片地址
        contentSub: '', // 反馈内容
        userPeople: '',
        userPhone: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

    },
    // 打开相册等
    fnUpImg: function() {
        const that = this;
        wx.chooseImage({
            count: 3,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success(res) {
                const tempFilePaths = res.tempFilePaths;
                const obj = that.data.imgArray;
                // 上传图片
                wx.showLoading({
                    title: '上传中',
                })
                wx.uploadFile({
                    url: _Server.myUpimg,
                    filePath: tempFilePaths[0],
                    name: 'image',
                    formData: {
                        token: encodeURI(wx.getStorageSync('token')),
                    },
                    success(res) {
                        const data = JSON.parse(res.data);
                        if (data.code == 0) {
                            obj.push(tempFilePaths[0]);
                            that.setData({
                                imgArray: obj
                            });
                            that.data.imgUpArray.push(data.data);
                            if (that.data.imgArray.length >= 3) {
                                that.setData({
                                    upImgBool: false
                                });
                            }
                        } else {
                            wx.showToast({
                                title: data.msg,
                                icon: 'none'
                            });
                        }
                    },
                    complete(){
                        setTimeout(function () {
                            wx.hideLoading()
                        }, 1000);
                    }
                });
            }
        })
    },
    // 删除
    fnCancel: function(e) {
        var that = this;
        var index = e.target.dataset.index;
        var obj = that.data.imgArray;
        obj.splice(index, 1);
        that.setData({
            imgArray: obj
        });

        var objUp = that.data.imgUpArray;
        objUp.splice(index, 1);

        if (that.data.imgArray.length < 3) {
            that.setData({
                upImgBool: true
            });
        }
    },
    // 预览
    fnPreviewImg: function(e) {
        const index = e.target.dataset.index;
        const that = this;
        var imgs = [];
        for (let i = 0; i < that.data.imgArray.length; i++) {
            imgs.push(that.data.imgArray[i].src);
        }
        wx.previewImage({
            current: that.data.imgArray[index].src, // 当前显示图片的http链接
            urls: imgs // 需要预览的图片http链接列表
        });
    },
    // 提交
    fnSubmit: function() {
        var that = this;
        var imgs = that.data.imgUpArray;
        _ajax.myProblem(that.data.userPeople, imgs, that.data.userPhone,function (data) {
            if (data.code == 0) {
                wx.showToast({
                    title: data.msg,
                    icon: 'success',
                    duration: 2000
                });
                setTimeout(function(){
                    wx.navigateBack({});
                }, 800);
            }
        }, this);
    },
    contentSub: function(e) {
        this.setData({
            userPeople: e.detail.value
        });
    },
    phoneSub: function(e) {
        this.setData({
            userPhone: e.detail.value
        });
    },
})